﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Helpers.Extensions
{
    public static class StringExt
    {
        #region string流
        public static bool IsNullOrEmpty(this string @this)
        {
            return string.IsNullOrEmpty(@this);
        }

        public static bool IsNotNullOrEmpty(this string @this)
        {
            return !IsNullOrEmpty(@this);
        }
        #endregion

        #region regex
        public static bool IsMatch(this string @this, string pattern, RegexOptions options = RegexOptions.None)
        {
            return Regex.IsMatch(@this ?? string.Empty, pattern, options);
        }
        public static Match GetMatch(this string @this, string pattern, RegexOptions options = RegexOptions.None)
        {
            return Regex.Match(@this ?? string.Empty, pattern, options);
        }
        public static string GetReplace(this string @this, string pattern, string replacement, RegexOptions options = RegexOptions.None)
        {
            return Regex.Replace(@this ?? string.Empty, pattern, replacement, options);
        }


        public static MatchCollection GetMatches(this string @this, string pattern, RegexOptions options = RegexOptions.None)
        {
            return Regex.Matches(@this ?? string.Empty, pattern, options);
        }
        #endregion

        #region parse
        public static int? TryParseInt(this string @this)
        {
            if (int.TryParse(@this, out var value))
                return value;
            else
                return null;
        }

        public static double? TryParseDouble(this string @this)
        {
            if (double.TryParse(@this, out var value))
                return value;
            else
                return null;
        }

        public static DateTime? TryParseDateTime(this string @this)
        {
            if (DateTime.TryParse(@this, out var value))
                return value;
            else
                return null;
        }
        #endregion

        #region HtmlAgilityPack
        /// <summary>
        /// 获取DocumentNode
        /// </summary>
        /// <param name="this"></param>
        /// <returns></returns>
        public static HtmlAgilityPack.HtmlNode GetDocumentNode(this string @this)
        {
            HtmlAgilityPack.HtmlDocument htmlDocument = new HtmlAgilityPack.HtmlDocument();
            htmlDocument.LoadHtml(@this);
            return htmlDocument.DocumentNode;
        }
        #endregion

        #region 网页加密解密
        public static string HtmlDecode(this string @this)
        {
            if (String.IsNullOrEmpty(@this))
                return @this;
            return System.Net.WebUtility.HtmlDecode(@this);
        }
        public static string UrlEncode(this string @this, Encoding encoding = null)
        {
            if (String.IsNullOrEmpty(@this))
                return @this;
            return System.Web.HttpUtility.UrlEncode(@this, encoding ?? Encoding.UTF8)
                //.Replace("+", "%20")
                //针对腾讯api签名
                .Replace("!", "%21")
                .Replace("*", "%2A")
                .Replace("(", "%28")
                .Replace(")", "%29");
        }

        /// <summary>
        /// URL编码算法用大写字母
        /// </summary>
        /// <param name="this"></param>
        /// <returns></returns>
        public static string UrlEncodeAndUpper(this string @this, Encoding encoding = null)
        {
            if (String.IsNullOrEmpty(@this))
                return @this;
            var s = UrlEncode(@this, encoding ?? Encoding.UTF8);
            return Regex.Replace(s, @"%\w\w", it => it.Value.ToUpper());
        }
        #endregion
    }
}
